package cn.com.infcn.monitor.task.impl;

import org.apache.mina.core.session.IoSession;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Who;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.infcn.monitor.bean.IFCResponseBean;
import cn.com.infcn.monitor.bean.IFCUser;
import cn.com.infcn.monitor.util.IFCBeanUtils;
import cn.com.infcn.monitor.util.IFCMinitorConstanst;

/**
 * 获取系统用户相关信息
 * 
 * @author jjs
 *
 */
public class UserTask extends BaseTask {

	private final static Logger logger = LoggerFactory.getLogger(UserTask.class);

	public UserTask(IoSession session, Object message) {
		super(session, message);
	}

	@Override
	public void execute() throws Exception {
		IFCResponseBean result = user();
		sendResponse(result);
	}

	private IFCResponseBean user() throws SigarException {
		IFCResponseBean result = new IFCResponseBean();

		try {
			Sigar sigar = new Sigar();
			Who whos[] = sigar.getWhoList();
			IFCUser[] users = null;
			if (whos != null && whos.length > 0) {
				users = new IFCUser[whos.length];
				for (int i = 0; i < whos.length; i++) {
					Who who = whos[i];
					IFCUser user = new IFCUser();
					IFCBeanUtils.copyProperties(who, user);
					users[i] = user;
				}
			}

			result.setResult(users);
			result.setResponseCode(IFCMinitorConstanst.MONITOR_RESPONSE_CODE_OK);
		} catch (Exception e) {
			logger.error(e.getMessage());
			result.setResponseCode(IFCMinitorConstanst.MONITOR_RESPONSE_CODE_ERROR);
			result.setResponseMsg(e.getMessage());
		}
		return result;
	}

}
